package cn.maxhou.aop;

import org.aspectj.lang.JoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TransactionAspect {
    private static final Logger log = LoggerFactory.getLogger(TransactionAspect.class);

    /**
     * 前置增强-打开数据库链接开启事务
     */
    public void before(JoinPoint jb){
        log.info("[TransactionAspect前置增强】模拟打开数据库连接并开启事务.");
        log.info("SqlSession sqlSession=MyBatisUtil.createSqlSession(false)");
    }

    /**
     * 后置增强-执行事务提交
     */
    public void afterReturning(JoinPoint jb,Object result){
        log.info("[TransactionAspect 后置增强] 模拟正常完成业务提交事务...");
        log.info("session.commit();");
    }

    /**
     * 异常排除增强-执行事务回滚
     */
    public void afterThrowing(JoinPoint jb,Throwable e){
        log.info("【TransactionAspect异常抛出增强】模拟业务异常回滚事务,异常信息为:" +e.getMessage());
        log.info("session.rollback()");
    }
    /***
     * 最终增强-执行关闭数据库资源
     */
    public void after(JoinPoint jb){
        log.info("【TransactionAspect最终增强】模拟业务完成释放资源...");
        log.info("MyBatisUtil.closeSqlSession(sqlSession):");
    }
}
